package com.situ.blog.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.situ.blog.pojo.entity.User;
import com.situ.blog.pojo.query.UserQuery;
import com.situ.blog.service.IUserService;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.LayUITableResult;
import com.situ.blog.util.Result;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {
    private User user = new User();

    @Autowired
    private IUserService userService;

    public void login() {
        String code = ServletActionContext.getRequest().getParameter("code");
        HttpSession session = ServletActionContext.getRequest().getSession();
        String checkcode = (String) session.getAttribute("checkcode");
        if (!checkcode.equalsIgnoreCase(code)) {
            JSONUtil.toJSON(Result.error("验证码错误"));
            return;
        }

        User loginUser = userService.login(user.getName(), user.getPassword());
        if (loginUser == null) {
            JSONUtil.toJSON(Result.error("用户名或密码错误"));
        } else {
            session.setAttribute("user", loginUser);
            JSONUtil.toJSON(Result.ok("登录成功"));
        }
    }

    public String logout() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.invalidate();
        return "logout";
    }

    public void selectByPage() {
        HttpServletRequest request = ServletActionContext.getRequest();
        String pageStr = request.getParameter("page");
        int page = 1; // 默认显示第几页数据
        if (!StringUtils.isEmpty(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        int limit = 5; // 默认每页显示多少条记录
        String limitStr = request.getParameter("limit");
        if (!StringUtils.isEmpty(limitStr)) {
            limit = Integer.parseInt(limitStr);
        }
        //组装搜索条件
        String name = request.getParameter("name");
        String roleStr = request.getParameter("role");
        Integer role = StringUtils.isEmpty(roleStr) ? null : Integer.parseInt(roleStr);
        UserQuery userQuery = new UserQuery(page, limit, name, role);
        LayUITableResult layUITableResult = userService.selectByPage(userQuery);
        JSONUtil.toJSON(layUITableResult);
    }

    public void deleteById() {
        userService.deleteById(user);
        JSONUtil.toJSON(Result.ok("删除成功"));
    }

    public void add() {
        userService.add(user);
        JSONUtil.toJSON(Result.ok("添加成功"));
    }

    @Override
    public User getModel() {
        return user;
    }
}
